<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.shopping.user.*" %>
<%@ page import="com.shopping.service.*" %>
<%@ page import="java.util.*" %>
 <!DOCTYPE html>

<% 
	User u = (User)session.getAttribute("user");
	if(u != null ) {
		out.println("请先登录");
		//response.sendRedirect("userlogin.jsp");
		return;
	}
	
%>



<%	
	request.setCharacterEncoding("utf-8");
    //User u = (User)session.getAttribute("user");
    
	String action = request.getParameter("action");
			//System.out.println("id:" + u.getId());
	if(action !=null && action.equals("modify")) {

		if(u == null ) {
			//out.println("请先登录");
			response.sendRedirect("userlogin.jsp");
		}
		
		String username = u.getUsername();
		if(!username.equals(request.getParameter("user"))) {
			if(UserService.existUser(request.getParameter("user"))) {
				out.println("用户名已存在，请重新输入");
				return;
			}else {
				User u1 = new User();
				u1.setUsername(request.getParameter("user"));
				u1.setPassword(request.getParameter("pw1"));
				u1.setPhone(request.getParameter("phone"));
				u1.setAddr(request.getParameter("addr"));
				UserService.userChanged(u1,username);
				out.println("修改成功");
				response.sendRedirect("userlogin.jsp");
			}
		}else {
			User u1 = new User();
			u1.setUsername(request.getParameter("user"));
			u1.setPassword(request.getParameter("pw1"));
			u1.setPhone(request.getParameter("phone"));
			u1.setAddr(request.getParameter("addr"));
			UserService.userChanged(u1,username);
			//应该提交提示重新登录
			//response.sendRedirect("userlogin.jsp");
			out.println("修改成功，请重新登录");
		}
%>	
	
	   <a href="userlogin.jsp">重新登录</a>
	
<%	
	}	
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
  <script type="text/javascript">
  function checkdata() {
	  var username = form.user.value.trim();
	  if(username.length <= 0 || username.length > 5 ) {
		  alert("cuowu")
		  return false;
	  }
	  
	  var pw1 = form.pw1.value.trim();
	  if(pw1.length < 6 || pw1.lenght > 10) {
		  alert("密码必须是6-10位数")
		  return false;
	  }
	  
	  var phone = form.phone.value.trim();
	  if(phone.length != 11) {
		  alert("请输入正确的11位手机号码");
		  return false;
	  }
	  
	  var addr = form.addr.value.trim();
	  if(addr.length <= 0) {
		  alert("请输入地址");
		  return false;
	  }
  }
  </script>
</head>

<body>
  <form name="form" action="usermodify.jsp" method="post" >
      <input type="hidden" name="action" value="modify">
	  <table>
	    <tr>
	      <td>
	                      用户名：&nbsp;&nbsp;&nbsp;&nbsp;
	               <input type="text" name="user" size=30 value=<%=u.getUsername()%>>
	      </td>
	    </tr>
	    <tr>
	      <td>
	                      密码：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	             <input type="password" name="pw1" size=30 value=<%=u.getPassword()%>>
	      </td>
	    </tr>
	    <tr>
	      <td>
	                      手机号：&nbsp;&nbsp;&nbsp;&nbsp;
	               <input type="text" name="phone" size=30 value=<%=u.getPhone()%>>
	      </td>
	    </tr>
	    <tr>
	      <td>
	                      联系地址：<textarea name="addr" cols="30" rows="10" ><%=u.getAddr()%></textarea>
	      </td>
	    </tr>
	    <tr>
	      <td colspan="2">
	         <input type="submit" name="submit" value="提交" onclick="checkdata();"/>            
	      </td>
	    </tr>
	  </table>
	</form>
</body>
</html>